package com.akbar.controller;


import com.akbar.dto.LoginDto;
import com.akbar.properties.JwtProperties;
import com.akbar.utils.ResultUtil;
import com.akbar.service.AuthService;
import com.akbar.vo.UserVo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author akbar
 * @since 2025-07-21
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private AuthService authService;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 用户登录
     *
     */
    @PostMapping("/login")
    public ResultUtil<UserVo> login(@RequestBody @Valid LoginDto loginDto) {

        UserVo userVo = authService.login(loginDto);

        return ResultUtil.success(userVo);
    }


    /**
     * 退出登录
     */
    @GetMapping("/logout")
    public ResultUtil<Void> logout(HttpServletRequest request) {
        String token = request.getHeader(jwtProperties.getTokenName());
        if (StringUtils.hasText(token)) {
            redisTemplate.delete(token);
        }
        return ResultUtil.success(null);
    }
}
